<?php

class Kd_Aop_Manager {

    private static $_listeners = array();
    private static $_pointcutins = array();

    public static function initialize(){
//        $arr = Kd_Config::getSetting('aop.pointcutins');
//        if(!empty($arr)){
//            self::$_pointcutins = Kd_Config::getSetting('aop.pointcutins');
//        }
//
//        $arr = Kd_Config::getSetting('aop.listeners');
//        if(!empty($arr)){
//            foreach(Kd_Config::getSetting('aop.listeners') as $listener){
//                self::addListener($listener['name'], new Kd_Aop_Listener($listener['performer'], $listener['classMatcher'], $listener['methodMatcher']));
//            }
//        }
//
//        Kd_Aop_Compiler::compile();
//        Kd_Package::addIncludePath(Kd_Config::getTmpPath().'aop'.DIRECTORY_SEPARATOR);
    }

//    public static function getIgnoreMethods(){
//        return self::$_configs['ignoreMethods'];
//    }

    public static function getPointcutins(){
        return self::$_pointcutins;
    }

//    public static function getCompiledPath(){
//        return self::$_configs['compiledPath'];
//    }

    public static function isPointcutin($className){
        foreach(self::$_pointcutins as $pointcutin){
            if(preg_match("/{$pointcutin['target']}/i", $className)){
                return TRUE;
            }
        }

        return FALSE;
    }

    public static function isListeningTarget($className){
        foreach(self::$_listeners as $listener){
            if(preg_match("/{$listener->getClassMatcher()}/i", $className)){
                return TRUE;
            }
        }

        return FALSE;
    }

//    public static function proxyExists($className){
//
//    }

    public static function getListeners(){
        return self::$_listeners;
    }

    public static function addListener($name, $listener){
        self::$_listeners[$name] = $listener;
    }

    public static function removeListener(){
        $args = func_get_args();
        foreach($args as $name){
            if(isset(self::$_listeners[$name])){
                unset(self::$_listeners[$name]);
            }
        }
    }

    public static function clearListeners(){
        self::$_listeners = array();
    }
}